<?php
/* SVN FILE: $Id$ */
/**
 *  functions.php   The default functions file for the CP Construction Theme
 *  
 *  This file is the main part of the CP Construction Wordpress Theme functions.
 *  It is home to the main functions necessary for the theme to work correctly.
 *
 *  @package        CPConstruction
 *  @subpackage     Functions
 *  @copyright      CP Construction (Gwent) Ltd <http://www.cpconstruction.co.uk/> 2012
 *  @author         Richard Perry <http: //www.perry-online.me.uk/>
 *  @since          Release 2.0.0
 *  @version        $Rev$
 *  @modifiedby     $LastChangedBy$
 *  @lastmodified   $Date$
 *
 *  @todo           ToDo List
 *                  -
 */


// Add the theme options functions
require_once ( get_template_directory() . '/theme-options.php' );
 
// Make theme available for translation
// Translations can be filed in the /languages/ directory
load_theme_textdomain( 'cpcons', TEMPLATEPATH . '/languages' );
 
$locale = get_locale();
$locale_file = TEMPLATEPATH . "/languages/$locale.php";
if ( is_readable($locale_file) )
    require_once($locale_file);
 
// Get the page number
function get_page_number() {
    if ( get_query_var('paged') ) {
        print ' | ' . __( 'Page ' , 'your-theme') . get_query_var('paged');
    }
} // end get_page_number
 
 
 
 
 
 
 

 if ( ! function_exists( 'cpc_setup' ) ):
/**
 *  Set up theme defaults and registers support for various WordPress features.
 *
 *  @uses   add_editor_style() To style the visual editor.
 *  @uses   add_theme_support() To add support for post thumbnails, automatic 
 *          feed links, and Post Formats.
 *  @uses   register_nav_menus() To add support for navigation menus.
 * 
 *  @since  Release 0.1.1
 */
function cpc_setup() {

    // This theme styles the visual editor with editor-style.css to match the 
    // theme style.
    add_editor_style();
    
    // Add default posts and comments RSS feed links to <head>.
    add_theme_support( 'automatic-feed-links' );

    // Register the menus for the theme
    register_nav_menus(
        array(
          'main-menu' => __( 'Main Menu' ),
          'footer-menu' => __( 'Footer Menu' )
        )
    );

    // Add support for a variety of post formats
    add_theme_support( 'post-formats', 
        array( 
            'aside', 
            'audio', 
            'chat', 
            'gallery', 
            'image', 
            'link', 
            'quote', 
            'status', 
            'video' 
        ) 
    );

    // This theme uses Featured Images (also known as post thumbnails) for 
    // per-post/per-page Custom Header images
    add_theme_support( 'post-thumbnails' );

    // This theme uses custom backgrounds
    add_custom_background();

}
endif; // cpc_setup
add_action( 'after_setup_theme', 'cpc_setup' );

/**
 * Register our sidebars and widgetized areas.
 *
 * @since Release 0.1.0
 */
function cpc_widgets_init() {

    register_sidebar( array(
        'name' => __( 'Page Sidebar', 'cpc' ),
        'id' => 'page_widget_area',
        'before_widget' => '<aside id="%1$s" class="widget %2$s">',
        'after_widget' => "</aside>",
        'before_title' => '<h4 class="widget-title">',
        'after_title' => '</h4>',
    ) );

    register_sidebar( array(
        'name' => __( 'Post Sidebar', 'cpc' ),
        'id' => 'post_widget_area',
        'before_widget' => '<aside id="%1$s" class="widget %2$s">',
        'after_widget' => "</aside>",
        'before_title' => '<h4 class="widget-title">',
        'after_title' => '</h4>',
    ) );

    register_sidebar( array(
        'name' => __( 'Front Page Sidebar', 'cpc' ),
        'id' => 'front_page_widget_area',
        'before_widget' => '<aside id="%1$s" class="widget %2$s">',
        'after_widget' => "</aside>",
        'before_title' => '<h4 class="widget-title">',
        'after_title' => '</h4>',
    ) );

    register_sidebar( array(
        'name' => __( 'Footer Sidebar', 'cpc' ),
        'id' => 'sidebar-foot',
        'description' => __( 'An optional widget area for the footer', 'cpc' ),
        'before_widget' => '<aside id="%1$s" class="widget %2$s">',
        'after_widget' => "</aside>",
        'before_title' => '<h4 class="widget-title">',
        'after_title' => '</h4>',
    ) );
}
add_action( 'widgets_init', 'cpc_widgets_init' );

/**
 *  Change the default excerpt length so post summaries are more readable
 * 
 *  @param  integer number of words to include in the excerpt
 */
function cpc_custom_excerpt( $length ) {
    return 75;
}
add_filter( 'excerpt_length', 'cpc_custom_excerpt', 9999 );

/**
 *  Change the [...] to a Read More link
 */
function cpc_excerpt_more($more) {
    global $post;
    return '<br /><a href="'. get_permalink($post->ID) . '" class="alignright">Read more...</a>';
}
add_filter('excerpt_more', 'cpc_excerpt_more');


/**
 *  Register the Javascript files for the theme and enqueue as necessary
 */
function cpc_javascript_init() {
    if ( !is_admin() ) {
        // Change the Wordpress jQuery reference to use the Google CDN jQuery library 
        wp_deregister_script( 'jquery' ); // unregistered key jQuery
        wp_register_script( 'jquery', 
           'http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js', 
           false, '1.4.2'); // register key jQuery with URL of Google CDN
        wp_enqueue_script( 'jquery' ); // include jQuery
        
        // Register and enqueue the rotating banner script
        wp_register_script('banner',
            get_template_directory_uri() . '/scripts/banner.js',
            array('jquery'),'1.0');  
        wp_enqueue_script('banner');  
    }
}
add_action( 'after_setup_theme', 'cpc_javascript_init' ); 


/**
 * Remove the automatic paragraph wrapper around images
 */
function cpc_unautop_4_img( $content ) {
    $content = preg_replace(
        '/<p>\\s*?(<a rel=\"attachment.*?><img.*?><\\/a>|<img.*?>)?\\s*<\\/p>/s',
        '<figure>$1</figure>',
        $content
    );
    return $content;
}
add_filter( 'the_content', 'cpc_unautop_4_img', 999 );



/**
 *  ADMIN & LOGIN FUNCTIONS
 */

/**
 * Auto-subscribe or unsubscribe an Edit Flow user group when a post changes status
 *
 * @see http://editflow.org/extend/auto-subscribe-user-groups-for-notifications/
 *
 * @param string $new_status New post status
 * @param string $old_status Old post status (empty if the post was just created)
 * @param object $post The post being updated
 * @return bool $send_notif Return true to send the email notification, return false to not
 */
function efx_auto_subscribe_usergroup( $new_status, $old_status, $post ) {
    global $edit_flow;

    // When the post is first created, you might want to automatically set
    // all of the user's user groups as following the post
    //if ( 'draft' == $new_status ) {
        // Get all of the user groups for this post_author
        //$usergroup_ids_to_follow = $edit_flow->user_groups->get_usergroups_for_user( $post->post_author );
        //$usergroup_ids_to_follow = array_map( 'intval', $usergroup_ids_to_follow );
        //$edit_flow->notifications->follow_post_usergroups( $post->ID, $usergroup_ids_to_follow, true );
    //}

    // You could also follow a specific user group based on post_status
    if ( 'pending' == $new_status ) {
        // You'll need to get term IDs for your user groups and place them as
        // comma-separated values
        $usergroup_ids_to_follow = array(
                24
            );
        $edit_flow->notifications->follow_post_usergroups( $post->ID, $usergroup_ids_to_follow, true );
    }

    // Return true to send the email notification
    return true;
}
add_filter( 'ef_notification_status_change', 'efx_auto_subscribe_usergroup', 10, 3 );
 




/**
 *  FUNCTIONS FOR USE WITHIN THE THEME
 */

/**
 *  Generate next/previous navigation for use anywhere within the theme
 *  
 *  @param  nav_id      an ID tag for the navigation element
 */
function cpc_content_nav( $nav_id ) {
    global $wp_query;

    if ( $wp_query->max_num_pages > 1 ) : ?>
        <nav id="<?php echo $nav_id; ?>">
            <h3 class="assistive-text">Post navigation</h3>
            <div class="nav-previous alignleft">
                <?php next_posts_link( '&larr; Previous post' ); ?>
            </div>
            <div class="nav-next alignright">
                <?php previous_posts_link( 'Neext post &rarr;' ); ?>
            </div>
        </nav>
    <?php endif;
}

/**
 *  Generates the meta data relating to the post
 */
function cpc_entry_meta() {
    printf( '<span class="meta-prep meta-prep-author">%1$s</span>
             <span class="author vcard">
                <a class="url fn n" href="%2$s" title="%3$s" rel="author">%4$s</a>
             </span>
             <span class="meta-sep"> | </span>
             <span class="meta-prep meta-prep-entry-date">%5$s</span>
             <time class="entry-date" datetime="%6$s" pubdate>%7$s</time>',
        esc_attr( 'By ', 'cpcons' ),
        esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ),
        esc_attr( sprintf( 'View all posts by %s',  get_the_author() ) ),
        get_the_author(),
        esc_attr( 'Published ', 'cpcons' ),
        esc_attr( get_the_time( 'c' ) ),
        esc_html( get_the_date() )
    );
}

/**
 *  Changes the string to uppercase
 */
function cpc_to_upper($content) {
    return strtoupper($content);
}

/**
 *  SHORTCODES
 */
 
/**
 *  Obscures an email when wrapped with the [mailto][/mailto] shortcode
 */
function cpc_obfuscate( $atts , $content=null ) {
   return antispambot($content);
}
add_shortcode('mailto', 'cpc_obfuscate');
